package com.rcm.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;






import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rcm.dao.CoursesinfoMapper;
import com.rcm.dao.StudentinfoMapper;
import com.rcm.dao.TeacherinfoMapper;
import com.rcm.model.Admininfo;
import com.rcm.model.Coursesinfo;
import com.rcm.model.Courstatistics;
import com.rcm.model.Studentinfo;
import com.rcm.model.Teacherinfo;
import com.rcm.utility.MyBatisTool;

/**
 * Servlet implementation class AddTeacherServlet
 */
@WebServlet("/Teacherinfocontroller/*")
public class Teacherinfocontroller extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private TeacherinfoMapper mapper4=null;

	//获取url
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		String uri = request.getRequestURI();
		int idx = uri.lastIndexOf("/");
		String methodName = uri.substring(idx + 1);
		System.out.println(methodName);

		try {
		String Teachername= (String) request.getSession().getAttribute("Teachername");
				if(Teachername!=null) {
		
			Method method = getClass().getDeclaredMethod(methodName, HttpServletRequest.class,
					HttpServletResponse.class);
			
			method.invoke(this, request, response);
				}else {
					out.print("<script language='javascript'>alert('请重新登录账号!');window.location.href='http://localhost:8080/ElectiveSystem/index.jsp';</script>");
					return;
				}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	protected void Addstudent(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

		try {
			request.setCharacterEncoding("UTF-8");
			response.setCharacterEncoding("UTF-8");
			response.setContentType("text/html;charset=utf-8");
			PrintWriter out = response.getWriter();
			SqlSession session=MyBatisTool.getSession();
			StudentinfoMapper mapper = session.getMapper(StudentinfoMapper.class);
			Map<String, String[]> map = request.getParameterMap();
			Studentinfo studentinfo = new Studentinfo();
			BeanUtils.populate(studentinfo, map);
			System.out.println(studentinfo);
			mapper.insert(studentinfo);

			session.close();
			studentlist(request,response);

		} catch (Exception e) {
			response.sendRedirect("/error.jsp");
			return;
		}

	}

	public void studentlist(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
	{	

		SqlSession session=MyBatisTool.getSession();
		StudentinfoMapper mapper = session.getMapper(StudentinfoMapper.class);
		int pageNum=1;
		if (request.getParameter("pageNum")!=null) {
			pageNum=Integer.parseInt(request.getParameter("pageNum"));
		}

		PageHelper.startPage(pageNum,10);
		List<Studentinfo> studentlist = mapper.selectAll();
		session.close();
		PageInfo<Studentinfo> pageInfo=new  PageInfo<Studentinfo>(studentlist);
		System.out.println(studentlist);
		request.setAttribute("pageInfo", pageInfo);
		request.getRequestDispatcher("/studentlist.jsp").forward(request, response);

	}

	public void deletestudent(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
	{

		SqlSession session=MyBatisTool.getSession();
		StudentinfoMapper mapper = session.getMapper(StudentinfoMapper.class);
		String sid=request.getParameter("sid");
		mapper.deleteByPrimaryKey(sid);
	
		session.close();
		studentlist(request,response);
		
		
	}

	public void querystudent(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException, IllegalAccessException, InvocationTargetException
	{

		SqlSession session=MyBatisTool.getSession();
		StudentinfoMapper mapper = session.getMapper(StudentinfoMapper.class);
		String search = request.getParameter("search");
		String param = request.getParameter("param");
		Studentinfo querystudent=new Studentinfo();
		switch (param) {
		case "学号":
			querystudent.setSid(search);
			break;
		case "姓名":
			querystudent.setName(search);
			break;
		case "密码":
			querystudent.setPassword(search);
			break;
		case "邮箱":
			querystudent.setEmail(search);
			break;
		case "班级":
			querystudent.setGradeclass(search);
			break;
		case "年龄":
			querystudent.setAge(search);
			break;
		}
		
		PageHelper.startPage(1,10);
		List<Studentinfo> list=mapper.selectList(querystudent);
		System.out.println(list);
		PageInfo<Studentinfo> pageInfo=new PageInfo<Studentinfo>(list);
		session.close(); request.setAttribute("pageInfo", pageInfo);
		request.getRequestDispatcher("/studentlist.jsp").forward(request, response);


	}
	Coursesinfo querycourses=new Coursesinfo();;
	public void querycourses(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException, IllegalAccessException, InvocationTargetException
	{

		SqlSession session=MyBatisTool.getSession();
		CoursesinfoMapper mapper = session.getMapper(CoursesinfoMapper.class);
			Map map = request.getParameterMap();
			BeanUtils.populate(querycourses, map);
		
		
		request.getSession().setAttribute("s", querycourses);
		
		int pageNum=1;
		if (request.getParameter("pageNum")!=null) {
			pageNum=Integer.parseInt(request.getParameter("pageNum"));
		}
		PageHelper.startPage(pageNum,10);
		List<Coursesinfo> list=mapper.selectList(querycourses);
		session.close();
		PageInfo<Coursesinfo> pageInfo=new PageInfo<Coursesinfo>(list);
		request.setAttribute("pageInfo", pageInfo);
request.getRequestDispatcher("/courlist.jsp").forward(request, response);

	}


	public void update(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
	{

		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		SqlSession session=MyBatisTool.getSession();
		StudentinfoMapper mapper = session.getMapper(StudentinfoMapper.class);
		String sid=request.getParameter("sid");
		List<Studentinfo> studentlist = mapper.selectAll();
		for(Studentinfo c:studentlist){
			if(c.getSid().equals(sid)) {
				request.setAttribute("c", c);
				request.getRequestDispatcher("/studentInfoedit2.jsp").forward(request, response);
			}

		}

		session.close();


	}



	public void updatestudent(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
	{

		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		SqlSession session=MyBatisTool.getSession();
		StudentinfoMapper mapper = session.getMapper(StudentinfoMapper.class);
		Map<String, String[]> map = request.getParameterMap();
		Studentinfo studentinfo = new Studentinfo();
		try {
			BeanUtils.populate(studentinfo, map);
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		mapper.updateStudent(studentinfo); 

		session.close();
		studentlist(request,response);


	}
	public void courseList(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
	{

		SqlSession session=MyBatisTool.getSession();
		CoursesinfoMapper mapper = session.getMapper(CoursesinfoMapper.class);
		int pageNum=1;
		if (request.getParameter("pageNum")!=null) {
			pageNum=Integer.parseInt(request.getParameter("pageNum"));
		}
		PageHelper.startPage(pageNum,10);
		List<Coursesinfo> coursesinfo = mapper.selectAll();
		session.close();
		PageInfo<Coursesinfo> pageInfo=new PageInfo<Coursesinfo>(coursesinfo);
		request.setAttribute("pageInfo", pageInfo);
		request.getRequestDispatcher("/courlist.jsp").forward(request, response);
		

	}
	
	
	//选课结果图表
	public void courstatistics(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException, IllegalAccessException, InvocationTargetException
	{	
		System.out.println("选课图表!");
		SqlSession session=MyBatisTool.getSession();
		CoursesinfoMapper mapper = session.getMapper(CoursesinfoMapper.class);
		List<Courstatistics> Courstatistics = mapper.selectStatistics();
		System.out.println(Courstatistics);
		session.close();
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out =response.getWriter();
		String data=StringUtils.strip(Courstatistics.toString(),"[]");//第一个参数放集合，第二个参数去掉中括号"[]"
		out.print(data);


	}

	
	

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
